﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using XianboSite.Common;

namespace XianboSite.Models
{
    public class DataAccess
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IList<User> GetUsers()
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from User where 1=1";
            //string hql = "from User where id=:id";
            IQuery query = session.CreateQuery(hql);
            //query.SetParameter("liveid", "abcdefgh12");
            query.SetFirstResult(0);
            query.SetMaxResults(10);
            IList<User> list = query.List<User>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static User GetUser(string name, string password)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from User where UserName=:name and Password=:password";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("name", name);
            query.SetParameter("password", password);
            return query.UniqueResult<User>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IList<LiveDetails> GetLiveDetails()
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from LiveDetails where 1=1 order by PublishTime desc";
            //string hql = "from User where id=:id";
            IQuery query = session.CreateQuery(hql);
            //query.SetParameter("liveid", "abcdefgh12");
            query.SetFirstResult(0);
            query.SetMaxResults(10);
            IList<LiveDetails> list = query.List<LiveDetails>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastTime"></param>
        /// <param name="liveid"></param>
        /// <returns></returns>
        public static IList<LiveDetails> GetLiveDetails(DateTime lastTime, string liveid)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from LiveDetails where PublishTime>:publishtime and LiveID=:liveid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("publishtime", lastTime);
            query.SetParameter("liveid", liveid);
            IList<LiveDetails> list = query.List<LiveDetails>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastTime"></param>
        /// <param name="liveid"></param>
        /// <returns></returns>
        public static IList<UserComment> GetCommnets(string liveid)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from UserComment where LiveID=:liveid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("liveid", liveid);
            IList<UserComment> list = query.List<UserComment>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="liveid"></param>
        /// <param name="first"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<LiveDetails> GetLiveDetails(string liveid, int first, int max)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from LiveDetails where LiveID=:liveid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("liveid", liveid);
            query.SetFirstResult(first);
            query.SetMaxResults(max);
            IList<LiveDetails> list = query.List<LiveDetails>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IList<LiveCategory> GetLiveCategories()
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from LiveCategory where 1=1";
            IQuery query = session.CreateQuery(hql);
            IList<LiveCategory> list = query.List<LiveCategory>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="liveid"></param>
        /// <returns></returns>
        public static Live GetLive(string liveid)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from Live where LiveID=:liveid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("liveid", liveid);

            return query.UniqueResult<Live>();
        }

        //public static IList<Live> GetLives()
        //{
        //    ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
        //    string hql = "from Live where 1=1";
        //    //string hql = "from User where id=:id";
        //    IQuery query = session.CreateQuery(hql);
        //    //query.SetParameter("liveid", "abcdefgh12");
        //    query.SetFirstResult(0);
        //    query.SetMaxResults(10);
        //    IList<Live> list = query.List<Live>();

        //    return list;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="categoryname"></param>
        /// <param name="first"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<Live> GetLives(string categoryname, int first = 0, int max = 100)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from Live where CategoryName=:categoryname";
            //string hql = "from User where id=:categoryid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("categoryname", categoryname);
            query.SetFirstResult(first);
            query.SetMaxResults(max);
            IList<Live> list = query.List<Live>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<Live> GetLives(int max = 20)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from Live where 1=1 order by CreateTime desc";
            //string hql = "from User where id=:categoryid";
            IQuery query = session.CreateQuery(hql);
            query.SetMaxResults(max);
            IList<Live> list = query.List<Live>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<AttentionDetails> GetAttentionDetails(string userid, int max = 20)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from AttentionDetails where AttetionUserID=:auserid order by CreateTime desc";
            //string hql = "from User where id=:categoryid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("auserid", userid);
            query.SetMaxResults(max);
            IList<AttentionDetails> list = query.List<AttentionDetails>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="first"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<Live> GetUserLives(string userid, int first = 0, int max = 100)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from Live where UserID=:userid";
            IQuery query = session.CreateQuery(hql);
            query.SetParameter("userid", userid);
            query.SetFirstResult(first);
            query.SetMaxResults(max);
            IList<Live> list = query.List<Live>();

            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        public static void CreateUser(User user)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            ITransaction tran = session.BeginTransaction();
            session.Save(user);
            tran.Commit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="live"></param>
        public static void CreateLive(Live live)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            ITransaction tran = session.BeginTransaction();
            session.Save(live);
            tran.Commit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="details"></param>
        public static void CreateLiveDetails(LiveDetails details)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            ITransaction tran = session.BeginTransaction();
            session.Save(details);
            tran.Commit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="liveid"></param>
        /// <param name="userid"></param>
        public static void AddAttention(string liveid, string userid)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            ITransaction tran = session.BeginTransaction();
            Attention attention = new Attention()
            {
                AttentionID = RndIDHelper.GetRnd(10, true, true, true, false, string.Empty),
                AttetionUserID = userid,
                LiveID = liveid
            };
            session.SaveOrUpdateCopy(attention);
            tran.Commit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commnet"></param>
        public static void AddComment(UserComment commnet)
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            ITransaction tran = session.BeginTransaction();
            session.Save(commnet);
            tran.Commit();
        }
    }
}